package org.jeecg.modules.demo3.fly.utils;



import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.modules.demo3.fly.entity.tcp.Clents;
import org.jeecg.modules.demo3.fly.entity.tcp.DistributionDataBean;
import sun.security.util.Length;

import javax.imageio.stream.MemoryCacheImageInputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wjn
 * @version V1.0
 * @date 2021/5/27 18:07
 * @Copyright © 2021/5/27 深圳卓翼航空无人机有限公司
 */
     //数据保存类
    //使用 file 上传 保存在本地文件
public class FileInsert {

    private long MaxLength=10*1024*1024; // 10*1024*1024
    long size = 0;
    File file = null;
    ObjectMapper objectMapper = new ObjectMapper();

    public void WriterReader(DistributionDataBean distributionDataBean, int number, Clents clents){
        try {
            if(file != null){
                size = file.length(); //当文件已经存在 判断文件大小 是否超过10M 不然重新创建
            }else {
                System.out.println("创建文件");
                file = createFile(number); //获取文件
            }
            if(size >= MaxLength){ //如果文件大于 10M 重新创建文件
                System.out.println("大于10M后创建文件");
                file = createFile(number); // 创建新文件
            }
            FileWriter fileWriter = new FileWriter(file,true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            String DataBean = objectMapper.writeValueAsString(distributionDataBean); //将对象转换成json格式的字符串
            bufferedWriter.write(DataBean+"\n");
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("Error initializing stream");
        }
    }

    /**
     *  文件超过10M 后创建文件
     */

    public static File createFile(int number){
        String year = getCurrentYear();
        int month = getCurrentMonth();
        //编写储存地址
        String filePath = "C:\\OS\\fly\\"+number+"\\"+year+"\\"+month; //使用序列号 标识符做文档
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm"); // 使用系统当前时间为文件名
        Date date = new Date();
        String datas = simpleDateFormat.format(date);
        String pathName = datas+".txt"; //储蓄路劲
        File file = new File(filePath,pathName);
        if(!file.getParentFile().exists())
        {
            file.getParentFile().mkdirs();
        }
        if(file.exists()) {
            file.delete();
        }
        try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        return file;
    }

    public static String getCurrentYear(){ // 获取系统 年
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }

    public static int getCurrentMonth (){ // 获取系统 月
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH)+1;
        return month;
    }

}
